package com.k2data.k2app.service;

import com.google.common.collect.Lists;
import com.k2data.k2app.domain.po.*;
import com.k2data.k2app.mapper.SysTreeMapper;
import com.k2data.k2app.rest.SysClient;
import com.k2data.k2app.rest.domain.RectList;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by k2data on 17-12-11.
 */
@Log4j2
@Service
public class SysTreeService {

    @Autowired
    SysClient sysClient;

    private final SysTreeMapper sysTreeMapper;

    @Autowired
    public SysTreeService(SysTreeMapper sysTreeMapper){
        this.sysTreeMapper=sysTreeMapper;
    }

    public List<AgroupTree> getTree(long uid) {
        RectList rolesByUser = sysClient.getRolesByUser(uid);
        log.debug(rolesByUser);
        if (!rolesByUser.getResult().isEmpty()) {
            log.debug(rolesByUser.getResult().get(0).toString());
            boolean flag = rolesByUser.getResult().get(0).toString().contains("Administrator");
            log.debug(flag);
            Map role = (Map)rolesByUser.getResult().get(0);
            if(role.get("enname")!=null && role.get("enname").toString().equals("Administrator")){
                flag=true;
            }
            log.debug(flag);
            List<AgroupTree> gReList = Lists.newArrayList();
            List<AgroupTree> gList = sysTreeMapper.getGroup();
            for (AgroupTree agroupTree : gList) {
                List<CustomerTree> customerTreeList = Lists.newArrayList();
                if (flag) {
                    customerTreeList = sysTreeMapper.getCustomerAdmin(agroupTree.getGroupId());
                } else {
                    customerTreeList = sysTreeMapper.getCustomer(agroupTree.getGroupId(), uid);
                }

                if (!customerTreeList.isEmpty()) {
                    for (CustomerTree customerTree : customerTreeList) {
                        List<Integer> statuslist=Lists.newArrayList();//根据设备状态设置客户状态
                        List<EquipmentTree> equipmentTreeList = Lists.newArrayList();
                        if (customerTree.getCustomerNo().substring(0, 1).equals("t")) {
                            EquipmentTree equipment = new EquipmentTree();
                            equipment.setName("tcs");
                            equipmentTreeList.add(equipment);
                            for (EquipmentTree equipmentTree : equipmentTreeList) {
                                //根据设备编号查询客户的下的设备
                                //List<DeviceTree> deviceTreeList = sysTreeMapper.getDevicesTcs(customerTree.getCustomerNo());
                                //2018.1.23 修改为根据客户-设备对应表查询客户下的设备
                                List<DeviceTree> deviceTreeList = sysTreeMapper.getTcsDevices(customerTree.getId());
                                if (!deviceTreeList.isEmpty()) {
                                    for (DeviceTree deviceTree : deviceTreeList) {
                                        List<PointTree> pointTreeList = sysTreeMapper.getPoints(deviceTree.getId());
                                        deviceTree.setTreeList(pointTreeList);
                                        //判断设备状态
                                        switch (deviceTree.getStatus()) {
                                            case "不在线":
                                                statuslist.add(0);
                                                break;
                                            case "停机":
                                                statuslist.add(1);
                                                break;
                                            case "健康":
                                                statuslist.add(2);
                                                break;
                                            case "亚健康":
                                                statuslist.add(3);
                                                break;
                                            case "预警":
                                                statuslist.add(4);
                                                break;
                                            case "故障":
                                                statuslist.add(5);
                                                break;
                                        }
                                    }
                                    equipmentTree.setTreeList(deviceTreeList);
                                }
                            }
                            customerTree.setTreeList(equipmentTreeList);
                            //判断最高等级设备状态赋值给客户
                            if(statuslist.isEmpty()){
                                statuslist.add(0);
                            }
                            switch (Collections.max(statuslist)) {
                                case 0:
                                    customerTree.setStatus("不在线");
                                    break;
                                case 1:
                                    customerTree.setStatus("停机");
                                    break;
                                case 2:
                                    customerTree.setStatus("健康");
                                    break;
                                case 3:
                                    customerTree.setStatus("亚健康");
                                    break;
                                case 4:
                                    customerTree.setStatus("预警");
                                    break;
                                case 5:
                                    customerTree.setStatus("故障");
                                    break;
                            }
                        } else {
                            equipmentTreeList = sysTreeMapper.getEquipment(Long.parseLong(customerTree.getCustomerNo()));
                            if (!equipmentTreeList.isEmpty()) {
                                for (EquipmentTree equipmentTree : equipmentTreeList) {
                                    //根据设备编号查询客户的下的设备
                                    //List<DeviceTree> deviceTreeList = sysTreeMapper.getDevices(equipmentTree.getCustomerId(), equipmentTree.getEquipmentId());
                                    //2018.1.23 修改为根据客户-设备对应表查询客户下的设备
                                    //根据客户编号查询客户的ID
                                    List<DeviceTree> deviceTreeList=Lists.newArrayList();
                                    if(String.valueOf(equipmentTree.getCustomerId()).equals(customerTree.getCustomerNo())){
                                        long customerId=customerTree.getId();
                                        deviceTreeList = sysTreeMapper.getS8000Devices(customerId, equipmentTree.getEquipmentId());
                                    }
                                    for (DeviceTree deviceTree : deviceTreeList) {
                                        List<PointTree> pointTreeList = sysTreeMapper.getPoints(deviceTree.getId());
                                        deviceTree.setTreeList(pointTreeList);
                                        //判断设备状态
                                        switch (deviceTree.getStatus()) {
                                            case "停机":
                                                statuslist.add(1);
                                                break;

                                            case "不在线":
                                                statuslist.add(0);
                                                break;
                                            case "健康":
                                                statuslist.add(2);
                                                break;
                                            case "亚健康":
                                                statuslist.add(3);
                                                break;
                                            case "预警":
                                                statuslist.add(4);
                                                break;
                                            case "故障":
                                                statuslist.add(5);
                                                break;
                                        }
                                    }
                                    equipmentTree.setTreeList(deviceTreeList);
                                }
                            }
                            customerTree.setTreeList(equipmentTreeList);
                            customerTree.setTreeList(equipmentTreeList);
                            //判断最高等级设备状态赋值给客户
                            if(statuslist.isEmpty()){
                                statuslist.add(0);
                            }
                            switch (Collections.max(statuslist)) {
                                case 0:
                                    customerTree.setStatus("不在线");
                                    break;
                                case 1:
                                    customerTree.setStatus("停机");
                                    break;
                                case 2:
                                    customerTree.setStatus("健康");
                                    break;
                                case 3:
                                    customerTree.setStatus("亚健康");
                                    break;
                                case 5:
                                    customerTree.setStatus("故障");
                                    break;
                                case 4:
                                    customerTree.setStatus("预警");
                                    break;
                            }
                        }
                    }
                }
                agroupTree.setTreeList(customerTreeList);
            }
            for (AgroupTree agroupTree : gList) {
                if (!agroupTree.getTreeList().isEmpty()) {
                    gReList.add(agroupTree);
                }
            }
            return gReList;
        }
        return null;
    }


}
